package com.thf.config;

import com.alibaba.fastjson.JSON;
import com.thf.dao.test.TransactionTestDao;
import com.thf.model.test.TransactionTsetModel;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Controller
public class TransactionTest {

    @Resource
    TransactionTestDao transactionTestDao ;

    private int i = 0 ;

    @RequestMapping("/txTest")
    @ResponseBody
    @Transactional
    public String test(){
        TransactionTsetModel tx1 = new TransactionTsetModel();
        TransactionTsetModel tx2 = new TransactionTsetModel();
        List<TransactionTsetModel> list = new ArrayList<>();
        i+=500;
        tx1.setId(1);
        tx1.setMoney(new BigDecimal(5000+i));
        tx2.setId(2);
        tx2.setMoney(new BigDecimal(5000+i));
        Integer count1 = transactionTestDao.update(tx1);
        System.out.println(5/0);
        Integer count2 = transactionTestDao.update(tx2);
        if(count1<1 && count2<1){
            return "更新失败";
        }
        list.add(transactionTestDao.findById(tx1.getId()));
        list.add(transactionTestDao.findById(tx2.getId()));
        return JSON.toJSONString(list);
    }
}
